//
//  ParametersVCO.m
//  CocoaSynth
//
//  Created by Thomas Dupont on 21/02/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "ParametersVCO.h"

#import "ModuleVCOViewController.h"
#import "ModuleVCOProtocol.h"

@implementation ParametersVCO

@synthesize rangeSlider;
@synthesize frequencySlider;
@synthesize amountSlider;
@synthesize rangeText;
@synthesize frequencyText;
@synthesize amountText;
@synthesize resultText;
@synthesize module;

- (void)load
{
	range = 8;
	frequency = 0;
	amount = 0;
}

- (void)awakeFromNib
{
	[rangeSlider setIntValue:3];
	[self rangeChange:nil];
	[frequencySlider setFloatValue:3];
	[self frequencyChange:nil];
	[amountSlider setFloatValue:0];
	[self amountChange:nil];
}

- (IBAction)rangeChange:(id)sender
{
	switch ([rangeSlider intValue]) {
		case 0:
			[rangeText setTitleWithMnemonic:@"Low"];
			range = 64;
			break;
		case 1:
			[rangeText setTitleWithMnemonic:@"32"];
			range = 32;
			break;
		case 2:
			[rangeText setTitleWithMnemonic:@"16"];
			range = 16;
			break;
		case 3:
			[rangeText setTitleWithMnemonic:@"8"];
			range = 8;
			break;
		case 4:
			[rangeText setTitleWithMnemonic:@"4"];
			range = 4;
			break;
		case 5:
			[rangeText setTitleWithMnemonic:@"2"];
			range = 2;
			break;
		default:
			break;
	}
	[self refreshResult];
}

- (IBAction)frequencyChange:(id)sender
{
	[frequencyText setTitleWithMnemonic:[NSString stringWithFormat:@"%.f",[frequencySlider floatValue]]];
	frequency = [frequencySlider floatValue];
	[self refreshResult];
}

- (IBAction)amountChange:(id)sender
{
	[amountText setTitleWithMnemonic:[NSString stringWithFormat:@"%.f",[amountSlider floatValue]]];
	amount = [amountSlider floatValue];
	[self refreshResult];
}

- (void)refreshResult
{
	[(id<ModuleVCOProtocol>)[module model] setExponentialFrequency:(frequency + amount - log(range)/log(2))];
	float result = [(id<ModuleVCOProtocol>)[module model] baseFrequency];
	result = result * pow(2, [(id<ModuleVCOProtocol>)[module model] exponentialFrequency]);
	[resultText setTitleWithMnemonic:[NSString stringWithFormat:@"%.1f Hz", result]];
}

@end
